﻿using ExcelConfig;
using net_path.CSM.Host;
using net_path.data.Database;
using System.Text;

namespace net_path
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            Console.OutputEncoding = Encoding.UTF8;
            if (args.Length == 1 && args[0] == "cmd")
            {
                while (true)
                {
                    Console.Write("> ");
                    var cmd = Console.ReadLine();
                    if (string.IsNullOrEmpty(cmd)) continue;

                    switch (cmd.Trim())
                    {
                        case "help":
                        case "?":
                            Console.WriteLine("");
                            break;
                        case "app":
                            Console.Write("tag: ");
                            var tag = Console.ReadLine();
                            Console.Write("appid: ");
                            var appid = Console.ReadLine();
                            Console.Write("secret: ");
                            var secret = Console.ReadLine();
                            if (string.IsNullOrEmpty(tag) || string.IsNullOrEmpty(appid) || string.IsNullOrEmpty(secret)) continue;
                            var oks = 0;
                            using (var db = new NetPathDB())
                            {
                                //oks = db.UpdateWxApp(tag.Trim(), appid.Trim(), secret.Trim());
                            }
                            Console.WriteLine($"({oks}) OK!");
                            break;
                        case "db":
                            Console.Write("connect: ");
                            var connectstring = Console.ReadLine();
                            if (string.IsNullOrEmpty(connectstring)) continue;
                            AppConfig.WriteText("wx-api-db", connectstring.Trim());
                            break;
                    }
                }
            }
            try
            {
                IWebHost host = new WebHost();
                host.Start().Wait();
                while (host.State == TouchSocket.Sockets.ServerState.Running) Task.Delay(1000).Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
            Console.WriteLine("End");
        }
    }
}
